<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="gv-sub-content">
  <div class="gv-forms" layout="column">
    <h1>Tenants</h1>

    <blockquote>
      <p style="font-size: 12px">
        <ng-md-icon icon="info" style="fill: grey"></ng-md-icon>
        Copy/paste the ID to the API gateway configuration file in order to resolve API endpoints according to the configured tenant.
      </p>
    </blockquote>
    <div class="gv-form">
      <h2>Details</h2>
      <div class="gv-form-content" layout="column">
        <md-table-container>
          <table md-table ng-init="order = 'name'">
            <thead md-head md-order="order">
              <tr md-row>
                <th md-column md-order-by="id">ID</th>
                <th md-column md-order-by="name">Name</th>
                <th md-column md-order-by="description">Description</th>
                <th permission permission-only="'organization-tenant-d'"></th>
              </tr>
            </thead>
            <tbody md-body>
              <tr
                md-row
                ng-repeat="tenant in $ctrl.tenants | orderBy: order"
                ng-mouseover="hovered = true && tenant.id"
                ng-mouseout="hovered = false"
              >
                <td md-cell style="position: relative">
                  {{tenant.id}}
                  <span style="position: absolute; bottom: 8px">
                    <md-tooltip md-direction="right">Copy to clipboard</md-tooltip>
                    <ng-md-icon
                      ng-show="hovered"
                      icon="link"
                      ngclipboard
                      data-clipboard-text="{{tenant.id}}"
                      ngclipboard-success="$ctrl.onClipboardSuccess(e);"
                      role="button"
                    ></ng-md-icon>
                  </span>
                </td>
                <td
                  permission
                  permission-only="'organization-tenant-u'"
                  md-cell
                  ng-click="$ctrl.editName($event, tenant)"
                  ng-class="{'gravitee-placeholder': !tenant.name}"
                >
                  {{tenant.name || 'Add a name'}}
                </td>
                <td
                  permission
                  permission-only="'organization-tenant-u'"
                  md-cell
                  ng-click="$ctrl.editDescription($event, tenant)"
                  ng-class="{'gravitee-placeholder': !tenant.description}"
                >
                  {{tenant.description || 'Add a description'}}
                </td>
                <td permission permission-except="'organization-tenant-u'">{{tenant.name}}</td>
                <td permission permission-except="'organization-tenant-u'">{{tenant.description}}</td>
                <td md-cell permission permission-only="'organization-tenant-d'">
                  <ng-md-icon icon="delete" ng-click="$ctrl.deleteTenant(tenant)"></ng-md-icon>
                </td>
              </tr>
              <tr md-row permission permission-only="'organization-tenant-c'">
                <td md-cell></td>
                <td md-cell ng-click="$ctrl.newTenant($event)" class="gravitee-placeholder">New tenant</td>
                <td md-cell></td>
                <td md-cell></td>
              </tr>
            </tbody>
          </table>
        </md-table-container>
        <div permission permission-only="['organization-tenant-c', 'organization-tenant-u']">
          <md-button
            ng-click="$ctrl.saveTenants()"
            ng-disabled="!$ctrl.tenantsToCreate.length && !$ctrl.tenantsToUpdate.length"
            class="md-raised md-primary"
            >Save</md-button
          >
          <md-button ng-click="$ctrl.reset()" ng-disabled="!$ctrl.tenantsToCreate.length && !$ctrl.tenantsToUpdate.length" class="md-raised"
            >Reset</md-button
          >
        </div>
      </div>
    </div>
  </div>
</div>
